using OrderSystem.Common;
using OrderSystem.SystemFramework;
using System;
using System.Data;
using System.Data.SqlClient;

namespace OrderSystem.DataAccess
{
	public class Payments : IDisposable
	{
		private SqlDataAdapter dataAdapter;

		private SqlCommand selectCommand;

		private SqlCommand insertCommand;

		private SqlCommand updateCommand;

		private SqlCommand deleteCommand;

		public DataSet GetBalance(int nCustomerID)
		{
			DataSet dataSet = new DataSet("Balance");
			string text = "select case when sum(dbo.TotalValueOfOrder(o.Code)) is null then 0 else sum(dbo.TotalValueOfOrder(o.Code)) end from [Order] as o inner join Project as pj on o.ProjectID=pj.PKID inner join Customer as c on pj.CustomerID=c.PKID where c.PKID=@CustomerID";
			text += " select case when sum(TotalValue) is null then 0 else sum(TotalValue) end as pp from Payment where CustomerID=@CustomerID";
			try
			{
				this.selectCommand = new SqlCommand(text, new SqlConnection(AppSettings.ConnectionString));
				this.selectCommand.Connection.Open();
				this.selectCommand.Parameters.Add(new SqlParameter("@CustomerID", SqlDbType.Int));
				this.selectCommand.Parameters["@CustomerID"].Value = nCustomerID;
				this.dataAdapter = new SqlDataAdapter(this.selectCommand);
				this.dataAdapter.Fill(dataSet);
			}
			catch (SqlException ex)
			{
				throw ex;
			}
			return dataSet;
		}

		public DataTable LoadPaymment1(string sPaymentCode)
		{
			DataTable dataTable = new DataTable("Paymment");
			string cmdText = "SELECT pm.PKID AS 序号,pm.Code as 编码,pm.CustomerID AS 客户编号,c.CompanyName as 公司名称,pm.PaymentDate AS 付款日期,pm.PaymentProject AS 款项,cast(pm.TotalValue as decimal(18,2)) AS 付款金额,pm.WayOfPayment AS 付款方式,pm.PaymentAddress as 付款地址,pm.Remark as 备注,(select [Name] from Operator where PKID=pm.OperatorID) as 操作者 from [Payment] as pm inner join Customer as c on pm.CustomerID=c.PKID where pm.Code=@PaymentCode";
			try
			{
				this.selectCommand = new SqlCommand(cmdText, new SqlConnection(AppSettings.ConnectionString));
				this.selectCommand.Connection.Open();
				this.selectCommand.Parameters.Add(new SqlParameter("@PaymentCode", SqlDbType.VarChar, 32));
				this.selectCommand.Parameters["@PaymentCode"].Value = sPaymentCode;
				this.dataAdapter = new SqlDataAdapter(this.selectCommand);
				this.dataAdapter.Fill(dataTable);
			}
			catch (SqlException ex)
			{
				throw ex;
			}
			return dataTable;
		}

		public DataSet LoadPaymment(long nPaymentID)
		{
			DataSet dataSet = new DataSet("Paymment");
			string text = "SELECT pm.PKID AS 序号,pm.Code as 编号,pm.CustomerID AS 客户编号,c.CompanyName as 客户名称,pm.PaymentDate AS 付款日期,pm.PaymentProject AS 款项,cast(pm.TotalValue as Decimal(18,2)) AS 付款金额,pm.WayOfPayment AS 付款方式,pm.PaymentAddress as 付款地址,pm.Remark as 备注,(select [Name] from Operator where PKID=pm.OperatorID) as 操作者,pm.SettleMark as 结算标志,pm.SettleDate as 结算时间, case when pm.PaymentID is null then -1 else pm.PaymentID end as 结算父序号,cast(pm.Discount as Decimal(18,2)) as 优惠,cast(pm.SettledBalance as Decimal(18,2)) as 结算余额 from [Payment] as pm inner join Customer as c on pm.CustomerID=c.PKID where pm.PKID=@PaymentID";
			text += " SELECT Code as 订单号,dbo.TotalValueOfOrder(Code) as 金额 from [Order] where PaymentID=@PaymentID";
			text += " SELECT pm.PKID AS 序号, pm.Code as 编号,c.CompanyName AS 公司,c.Linkman as 联系人,pm.PaymentProject AS 款项,pm.PaymentDate AS 付款时间,cast(pm.TotalValue as decimal(10,2)) AS 金额,pm.WayOfPayment AS 付款方式,pm.PaymentAddress AS 付款地址,pm.Remark AS 备注,op.[Name] as 操作 FROM Payment AS pm INNER JOIN Customer AS c ON pm.CustomerID=c.PKID inner join Operator as op on pm.OperatorID=op.PKID where pm.PaymentID=@PaymentID and pm.SettleMark not like 'S'";
			try
			{
				this.selectCommand = new SqlCommand(text, new SqlConnection(AppSettings.ConnectionString));
				this.selectCommand.Connection.Open();
				this.selectCommand.Parameters.Add(new SqlParameter("@paymentID", SqlDbType.BigInt));
				this.selectCommand.Parameters["@paymentID"].Value = nPaymentID;
				this.dataAdapter = new SqlDataAdapter(this.selectCommand);
				this.dataAdapter.Fill(dataSet);
			}
			catch (SqlException ex)
			{
				throw ex;
			}
			return dataSet;
		}

		public DataTable GetPaymentsByCondition(PaymentCondition pc)
		{
			this.selectCommand = new SqlCommand();
			string text = "";
			if (pc.CustomerID > -1)
			{
				if (text == "")
				{
					text += "CustomerID=@CustomerID";
				}
				else
				{
					text += " AND CustomerID=@CustomerID";
				}
				this.selectCommand.Parameters.Add(new SqlParameter("@CustomerID", SqlDbType.Int));
				this.selectCommand.Parameters["@CustomerID"].Value = pc.CustomerID;
			}
			if (pc.PaymentDateStart != null)
			{
				if (text == "")
				{
					text += "PaymentDate>=CONVERT(DATETIME,@PaymentDateStart) AND PaymentDate<=CONVERT(DATETIME,@PaymentDateEnd)";
				}
				else
				{
					text += " AND PaymentDate>=CONVERT(DATETIME,@PaymentDateStart) AND PaymentDate<=CONVERT(DATETIME,@PaymentDateEnd)";
				}
				this.selectCommand.Parameters.Add(new SqlParameter("@PaymentDateStart", SqlDbType.VarChar, 32));
				this.selectCommand.Parameters["@PaymentDateStart"].Value = pc.PaymentDateStart;
				this.selectCommand.Parameters.Add(new SqlParameter("@PaymentDateEnd", SqlDbType.VarChar, 32));
				this.selectCommand.Parameters["@PaymentDateEnd"].Value = pc.PaymentDateEnd;
			}
			if (pc.PaymentProject != null)
			{
				if (text == "")
				{
					text += "PaymentProject LIKE '%'+@PaymentProject+'%'";
				}
				else
				{
					text += " AND PaymentProject LIKE '%'+@PaymentProject+'%'";
				}
				this.selectCommand.Parameters.Add(new SqlParameter("@PaymentProject", SqlDbType.NVarChar, 32));
				this.selectCommand.Parameters["@PaymentProject"].Value = pc.PaymentProject.Replace(" ", "%");
			}
			if (pc.WayOfPayment != null)
			{
				if (text == "")
				{
					text += "WayOfPayment LIKE '%'+@WayOfPayment+'%'";
				}
				else
				{
					text += " AND WayOfPayment LIKE '%'+@WayOfPayment+'%'";
				}
				this.selectCommand.Parameters.Add(new SqlParameter("@WayOfPayment", SqlDbType.NVarChar, 16));
				this.selectCommand.Parameters["@WayOfPayment"].Value = pc.WayOfPayment.Replace(" ", "%");
			}
			if (pc.PaymentAddress != null)
			{
				if (text == "")
				{
					text += "PaymentAddress LIKE '%'+@PaymentAddress+'%'";
				}
				else
				{
					text += " AND PaymentAddress LIKE '%'+@PaymentAddress+'%'";
				}
				this.selectCommand.Parameters.Add(new SqlParameter("@PaymentAddress", SqlDbType.NVarChar, 64));
				this.selectCommand.Parameters["@PaymentAddress"].Value = pc.PaymentAddress.Replace(" ", "%");
			}
			if (pc.Remark != null)
			{
				if (text == "")
				{
					text += "pm.Remark LIKE '%'+@Remark+'%'";
				}
				else
				{
					text += " AND pm.Remark LIKE '%'+@Remark+'%'";
				}
				this.selectCommand.Parameters.Add(new SqlParameter("@Remark", SqlDbType.NVarChar, 64));
				this.selectCommand.Parameters["@Remark"].Value = pc.Remark.Replace(" ", "%");
			}
			if (pc.SettleMark != null)
			{
				if (text == "")
				{
					text = text + "SettleMark in (" + pc.SettleMark + ")";
				}
				else
				{
					text = text + " AND SettleMark in (" + pc.SettleMark + ")";
				}
			}
			if (text != "")
			{
				text = " WHERE " + text;
			}
			DataTable dataTable = new DataTable("Payment");
			string commandText = "SELECT pm.PKID AS 序号, pm.Code as 编号,c.CompanyName AS 公司,c.Linkman as 联系人,pm.PaymentProject AS 款项,pm.PaymentDate AS 付款时间,cast(pm.TotalValue as decimal(10,2)) AS 金额,pm.WayOfPayment AS 付款方式,pm.PaymentAddress AS 付款地址,pm.Remark AS 备注,op.[Name] as 操作,case when SettleMark like 'S' then '已结算' when SettleMark like 'P' then '被结算' when SettleMark like 'N' then '未结算' else '作废' end as 结算状态,case when pm.SettleMark like 'S' then pm.SettleDate else '' end as 结算日期 FROM Payment AS pm INNER JOIN Customer AS c ON pm.CustomerID=c.PKID inner join Operator as op on pm.OperatorID=op.PKID" + text + " ORDER BY PaymentDate";
			try
			{
				this.selectCommand.Connection = new SqlConnection(AppSettings.ConnectionString);
				this.selectCommand.CommandText = commandText;
				this.selectCommand.Connection.Open();
				this.dataAdapter = new SqlDataAdapter(this.selectCommand);
				this.dataAdapter.Fill(dataTable);
			}
			catch (SqlException ex)
			{
				throw ex;
			}
			return dataTable;
		}

		public DataTable Get欠款()
		{
			DataTable dataTable = new DataTable("Paymment");
			string cmdText = "select c.PKID as 序号, c.CompanyName as 客户名称,case when (select top 1 cast(pm.SettledBalance as decimal(12,2)) from Payment as pm where pm.CustomerID=c.PKID and pm.SettleMark='S' order by SettleDate desc) is null then 0  else  (select top 1 cast(pm.SettledBalance as decimal(12,2)) from Payment as pm where pm.CustomerID=c.PKID and pm.SettleMark='S' order by SettleDate desc) end as 上次结算,case when (select sum(cast(TotalValue as decimal(12,2))) from Payment where SettleMark='N' and CustomerID=c.PKID) is null then 0 else (select sum(cast(TotalValue as decimal(12,2))) from Payment where SettleMark='N' and CustomerID=c.PKID) end as 未结算付款,sum(dbo.TotalValueOfOrder(o.Code)) as 未付款订单, cast(0 as decimal(12,2)) as 合计欠款 from [Order] as o inner join Project as pj on o.ProjectID=pj.PKID inner join Customer as c on pj.CustomerID=c.PKID where o.Payed=0 and o.Locked=1 and o.Cancelled=0 group by c.PKID, c.CompanyName order by c.PKID";
			try
			{
				this.selectCommand = new SqlCommand(cmdText, new SqlConnection(AppSettings.ConnectionString));
				this.selectCommand.Connection.Open();
				this.dataAdapter = new SqlDataAdapter(this.selectCommand);
				this.dataAdapter.Fill(dataTable);
			}
			catch (SqlException ex)
			{
				throw ex;
			}
			return dataTable;
		}

		public bool CancelSettle(long nPaymentID)
		{
			string cmdText = " SET XACT_ABORT ON declare @SettleMark varchar(2), @rowCount int select @SettleMark=SettleMark from Payment where PKID=@PaymentID if @SettleMark is null begin     raiserror('没找到此结算单',16,1)     return end if @SettleMark not like 'S' begin     raiserror('此单不是 已结算单',16,1)     return end select @rowCount=count(*) from Payment where SettleMark='S' and CustomerID=(select CustomerID from Payment where PKID=@PaymentID) and SettleDate>(select SettleDate from Payment where PKID=@PaymentID) if @rowCount is null or @rowCount<1 begin BEGIN TRANSACTION     update Payment set SettleMark='N',PaymentID=null,Discount=0,SettledBalance=0 where PaymentID=@PaymentID     update [Order] set Payed=0, PaymentID=null where PaymentID=@PaymentID COMMIT TRANSACTION end else     raiserror('不能取消结算，此结算单之后有结算单，请先取消后面的结算单！',16,1)";
			try
			{
				this.insertCommand = new SqlCommand(cmdText, new SqlConnection(AppSettings.ConnectionString));
				this.insertCommand.Connection.Open();
				this.insertCommand.Parameters.Add(new SqlParameter("@paymentID", SqlDbType.BigInt));
				this.insertCommand.Parameters["@paymentID"].Value = nPaymentID;
				this.insertCommand.ExecuteNonQuery();
			}
			catch (SqlException ex)
			{
				throw ex;
			}
			return true;
		}

		public bool Activate(long nPaymentID)
		{
			string cmdText = "UPDATE [Payment] SET SettleMark='N' WHERE PKID=@PaymentID";
			try
			{
				this.updateCommand = new SqlCommand(cmdText, new SqlConnection(AppSettings.ConnectionString));
				this.updateCommand.Connection.Open();
				this.updateCommand.Parameters.Add(new SqlParameter("@PaymentID", SqlDbType.BigInt));
				this.updateCommand.Parameters["@PaymentID"].Value = nPaymentID;
				this.updateCommand.ExecuteNonQuery();
			}
			catch (SqlException ex)
			{
				throw ex;
			}
			return true;
		}

		public bool Cancel(long nPaymentID)
		{
			string cmdText = " declare @SettleMark varchar(2) select @SettleMark=SettleMark from Payment where PKID=@PaymentID if @SettleMark is null begin     raiserror('没找到此结算单',16,1)     return end if @SettleMark like 'S' begin     raiserror('此单是 已结算单，请解除结算才能作废！',16,1)     return end if @SettleMark like 'P' begin     raiserror('此单是 被结算单，请解除结算才能作废！',16,1)     return end UPDATE [Payment] SET SettleMark='C' WHERE PKID=@PaymentID";
			try
			{
				this.updateCommand = new SqlCommand(cmdText, new SqlConnection(AppSettings.ConnectionString));
				this.updateCommand.Connection.Open();
				this.updateCommand.Parameters.Add(new SqlParameter("@PaymentID", SqlDbType.BigInt));
				this.updateCommand.Parameters["@PaymentID"].Value = nPaymentID;
				this.updateCommand.ExecuteNonQuery();
			}
			catch (SqlException ex)
			{
				throw ex;
			}
			return true;
		}

		public decimal GetLastSettleBalance(long nCurrentPaymentID, int nCustomerID, string sCurrentSettleDate)
		{
			string cmdText;
			if (nCurrentPaymentID < 0L)
			{
				cmdText = "select top 1 cast(SettledBalance as decimal(12,2)) from Payment where CustomerID=@CustomerID and SettleMark='S' order by SettleDate desc";
			}
			else
			{
				cmdText = "select top 1 cast(pm.SettledBalance as decimal(12,2)) from Payment as pm where pm.CustomerID=@CustomerID and pm.SettleMark='S' and pm.SettleDate<(select pm2.SettleDate from Payment as pm2 where pm2.PKID=@PaymentID) order by pm.SettleDate desc";
			}
			decimal result = 0m;
			try
			{
				this.selectCommand = new SqlCommand(cmdText, new SqlConnection(AppSettings.ConnectionString));
				this.selectCommand.Connection.Open();
				this.selectCommand.Parameters.Add(new SqlParameter("@CustomerID", SqlDbType.Int));
				this.selectCommand.Parameters["@CustomerID"].Value = nCustomerID;
				this.selectCommand.Parameters.Add(new SqlParameter("@PaymentID", SqlDbType.BigInt));
				this.selectCommand.Parameters["@PaymentID"].Value = nCurrentPaymentID;
				object obj = this.selectCommand.ExecuteScalar();
				if (obj != null && obj.ToString() != "")
				{
					result = Convert.ToDecimal(obj.ToString());
				}
			}
			catch (SqlException ex)
			{
				throw ex;
			}
			return result;
		}

		public long AddPayment(ref string sPaymentCode, int nCustomerID, DateTime dtPaymentDate, string paymentProject, double totalValue, string wayOfPayment, string paymentAddress, string remark, bool isSettle, long[] paymentID, decimal mDiscount, decimal m结算余额, string[] sOrderCode, int nOperatorID)
		{
			string text = "";
			if (paymentID != null)
			{
				for (int i = 0; i < paymentID.Length; i++)
				{
					if (text == "")
					{
						text += paymentID[i].ToString();
					}
					else
					{
						text = text + "," + paymentID[i].ToString();
					}
				}
				if (text != "")
				{
					text = " update Payment set SettleMark='P',SettleDate=@currentDate,PaymentID=@paymentID where PKID in (" + text + ")";
				}
			}
			string text2 = "";
			if (sOrderCode != null)
			{
				for (int i = 0; i < sOrderCode.Length; i++)
				{
					if (text2 == "")
					{
						text2 = text2 + "'" + sOrderCode[i] + "'";
					}
					else
					{
						text2 = text2 + ",'" + sOrderCode[i] + "'";
					}
				}
				if (text2 != "")
				{
					text2 = " update [Order] set Payed=1,PaymentID=@paymentID where Code in (" + text2 + ")";
				}
			}
			string text3 = " SET XACT_ABORT ON BEGIN TRANSACTION declare @currentDate datetime set @currentDate=GetDate() exec AddPayment @paymentID output, @paymentCode output, @CustomerID, @PaymentDate, @PaymentProject, @TotalValue, @WayOfPayment, @PaymentAddress, @Remark, @operatorID";
			if (isSettle)
			{
				string text4 = text3;
				text3 = string.Concat(new string[]
				{
					text4,
					text,
					text2,
					" update Payment set SettleMark='S', SettleDate=@currentDate, PaymentID=@paymentID, Discount=",
					mDiscount.ToString(),
					",SettledBalance=",
					m结算余额.ToString(),
					" where PKID=@paymentID COMMIT TRANSACTION"
				});
			}
			else
			{
				text3 += " COMMIT TRANSACTION";
			}
			long result = -1L;
			try
			{
				this.insertCommand = new SqlCommand(text3, new SqlConnection(AppSettings.ConnectionString));
				this.insertCommand.Connection.Open();
				this.insertCommand.Parameters.Add(new SqlParameter("@paymentID", SqlDbType.BigInt));
				this.insertCommand.Parameters["@paymentID"].Value = -1;
				this.insertCommand.Parameters["@paymentID"].Direction = ParameterDirection.InputOutput;
				this.insertCommand.Parameters.Add(new SqlParameter("@paymentCode", SqlDbType.VarChar, 32));
				this.insertCommand.Parameters["@paymentCode"].Value = "";
				this.insertCommand.Parameters["@paymentCode"].Direction = ParameterDirection.InputOutput;
				this.insertCommand.Parameters.Add(new SqlParameter("@CustomerID", SqlDbType.Int));
				this.insertCommand.Parameters.Add(new SqlParameter("@PaymentDate", SqlDbType.VarChar, 32));
				this.insertCommand.Parameters.Add(new SqlParameter("@PaymentProject", SqlDbType.NVarChar, 32));
				this.insertCommand.Parameters.Add(new SqlParameter("@TotalValue", SqlDbType.Money));
				this.insertCommand.Parameters.Add(new SqlParameter("@WayOfPayment", SqlDbType.NVarChar, 16));
				this.insertCommand.Parameters.Add(new SqlParameter("@PaymentAddress", SqlDbType.NVarChar, 64));
				this.insertCommand.Parameters.Add(new SqlParameter("@Remark", SqlDbType.NVarChar, 64));
				this.insertCommand.Parameters.Add(new SqlParameter("@OperatorID", SqlDbType.Int));
				this.insertCommand.Parameters["@CustomerID"].Value = nCustomerID;
				this.insertCommand.Parameters["@PaymentDate"].Value = dtPaymentDate.ToShortDateString();
				this.insertCommand.Parameters["@PaymentProject"].Value = paymentProject;
				this.insertCommand.Parameters["@TotalValue"].Value = totalValue;
				this.insertCommand.Parameters["@WayOfPayment"].Value = wayOfPayment;
				this.insertCommand.Parameters["@PaymentAddress"].Value = paymentAddress;
				this.insertCommand.Parameters["@Remark"].Value = remark;
				this.insertCommand.Parameters["@OperatorID"].Value = nOperatorID;
				this.insertCommand.ExecuteNonQuery();
				result = Convert.ToInt64(this.insertCommand.Parameters["@paymentID"].Value);
				sPaymentCode = this.insertCommand.Parameters["@paymentCode"].Value.ToString();
			}
			catch (SqlException ex)
			{
				throw ex;
			}
			return result;
		}

		public bool UpdatePayment(long nPaymentID, string paymentCode, int nCustomerID, DateTime dtPaymentDate, string paymentProject, double totalValue, string wayOfPayment, string paymentAddress, string remark, bool isSettle, long[] aPaymentID, decimal mDiscount, decimal m结算余额, string[] sOrderCode, int nOperatorID)
		{
			string text = "";
			if (aPaymentID != null)
			{
				for (int i = 0; i < aPaymentID.Length; i++)
				{
					if (text == "")
					{
						text += aPaymentID[i].ToString();
					}
					else
					{
						text = text + "," + aPaymentID[i].ToString();
					}
				}
				if (text != "")
				{
					text = " update Payment set SettleMark='P',SettleDate=@currentDate,PaymentID=@paymentID where PKID in (" + text + ")";
				}
			}
			string text2 = "";
			if (sOrderCode != null)
			{
				for (int i = 0; i < sOrderCode.Length; i++)
				{
					if (text2 == "")
					{
						text2 = text2 + "'" + sOrderCode[i] + "'";
					}
					else
					{
						text2 = text2 + ",'" + sOrderCode[i] + "'";
					}
				}
				if (text2 != "")
				{
					text2 = " update [Order] set Payed=1,PaymentID=@paymentID where Code in (" + text2 + ")";
				}
			}
			string text3 = " SET XACT_ABORT ON BEGIN TRANSACTION declare @currentDate datetime set @currentDate=GetDate() update Payment set CustomerID=@CustomerID,PaymentDate=CONVERT(DATETIME,@PaymentDate),PaymentProject=@PaymentProject, TotalValue=@TotalValue,WayOfPayment=@WayOfPayment,PaymentAddress=@PaymentAddress,Remark=@Remark where PKID=@paymentID";
			if (isSettle)
			{
				string text4 = text3;
				text3 = string.Concat(new string[]
				{
					text4,
					text,
					text2,
					" update Payment set SettleMark='S', SettleDate=@currentDate, PaymentID=@paymentID, Discount=",
					mDiscount.ToString(),
					",SettledBalance=",
					m结算余额.ToString(),
					" where PKID=@paymentID COMMIT TRANSACTION"
				});
			}
			else
			{
				text3 += " COMMIT TRANSACTION";
			}
			try
			{
				this.insertCommand = new SqlCommand(text3, new SqlConnection(AppSettings.ConnectionString));
				this.insertCommand.Connection.Open();
				this.insertCommand.Parameters.Add(new SqlParameter("@paymentID", SqlDbType.BigInt));
				this.insertCommand.Parameters["@paymentID"].Value = nPaymentID;
				this.insertCommand.Parameters.Add(new SqlParameter("@CustomerID", SqlDbType.Int));
				this.insertCommand.Parameters.Add(new SqlParameter("@PaymentDate", SqlDbType.VarChar, 32));
				this.insertCommand.Parameters.Add(new SqlParameter("@PaymentProject", SqlDbType.NVarChar, 32));
				this.insertCommand.Parameters.Add(new SqlParameter("@TotalValue", SqlDbType.Money));
				this.insertCommand.Parameters.Add(new SqlParameter("@WayOfPayment", SqlDbType.NVarChar, 16));
				this.insertCommand.Parameters.Add(new SqlParameter("@PaymentAddress", SqlDbType.NVarChar, 64));
				this.insertCommand.Parameters.Add(new SqlParameter("@Remark", SqlDbType.NVarChar, 64));
				this.insertCommand.Parameters.Add(new SqlParameter("@OperatorID", SqlDbType.Int));
				this.insertCommand.Parameters["@CustomerID"].Value = nCustomerID;
				this.insertCommand.Parameters["@PaymentDate"].Value = dtPaymentDate.ToShortDateString();
				this.insertCommand.Parameters["@PaymentProject"].Value = paymentProject;
				this.insertCommand.Parameters["@TotalValue"].Value = totalValue;
				this.insertCommand.Parameters["@WayOfPayment"].Value = wayOfPayment;
				this.insertCommand.Parameters["@PaymentAddress"].Value = paymentAddress;
				this.insertCommand.Parameters["@Remark"].Value = remark;
				this.insertCommand.Parameters["@OperatorID"].Value = nOperatorID;
				this.insertCommand.ExecuteNonQuery();
			}
			catch (SqlException ex)
			{
				throw ex;
			}
			return true;
		}

		public string AddPayment1(string paymentCode, int nCustomerID, DateTime dtPaymentDate, string paymentProject, double totalValue, string wayOfPayment, string paymentAddress, string remark, int nOperatorID)
		{
			string result = null;
			try
			{
				this.insertCommand = new SqlCommand("AddPayment", new SqlConnection(AppSettings.ConnectionString));
				this.insertCommand.CommandType = CommandType.StoredProcedure;
				this.insertCommand.Connection.Open();
				this.insertCommand.Parameters.Add(new SqlParameter("@paymentCode", SqlDbType.VarChar, 32));
				this.insertCommand.Parameters["@paymentCode"].Value = "";
				this.insertCommand.Parameters["@paymentCode"].Direction = ParameterDirection.InputOutput;
				this.insertCommand.Parameters.Add(new SqlParameter("@CustomerID", SqlDbType.Int));
				this.insertCommand.Parameters.Add(new SqlParameter("@PaymentDate", SqlDbType.VarChar, 32));
				this.insertCommand.Parameters.Add(new SqlParameter("@PaymentProject", SqlDbType.NVarChar, 32));
				this.insertCommand.Parameters.Add(new SqlParameter("@TotalValue", SqlDbType.Money));
				this.insertCommand.Parameters.Add(new SqlParameter("@WayOfPayment", SqlDbType.NVarChar, 16));
				this.insertCommand.Parameters.Add(new SqlParameter("@PaymentAddress", SqlDbType.NVarChar, 64));
				this.insertCommand.Parameters.Add(new SqlParameter("@Remark", SqlDbType.NVarChar, 64));
				this.insertCommand.Parameters.Add(new SqlParameter("@OperatorID", SqlDbType.Int));
				this.insertCommand.Parameters["@CustomerID"].Value = nCustomerID;
				this.insertCommand.Parameters["@PaymentDate"].Value = dtPaymentDate.ToShortDateString();
				this.insertCommand.Parameters["@PaymentProject"].Value = paymentProject;
				this.insertCommand.Parameters["@TotalValue"].Value = totalValue;
				this.insertCommand.Parameters["@WayOfPayment"].Value = wayOfPayment;
				this.insertCommand.Parameters["@PaymentAddress"].Value = paymentAddress;
				this.insertCommand.Parameters["@Remark"].Value = remark;
				this.insertCommand.Parameters["@OperatorID"].Value = nOperatorID;
				this.insertCommand.ExecuteNonQuery();
				result = this.insertCommand.Parameters["@paymentCode"].Value.ToString();
			}
			catch (SqlException ex)
			{
				throw ex;
			}
			return result;
		}

		public bool UpdatePayment1(long nPKID, int nCustomerID, DateTime dtPaymentDate, string paymentProject, double totalValue, string wayOfPayment, string paymentAddress, string remark)
		{
			try
			{
				this.insertCommand = new SqlCommand("UpdatePayment", new SqlConnection(AppSettings.ConnectionString));
				this.insertCommand.CommandType = CommandType.StoredProcedure;
				this.insertCommand.Connection.Open();
				this.insertCommand.Parameters.Add(new SqlParameter("@PKID", SqlDbType.BigInt));
				this.insertCommand.Parameters.Add(new SqlParameter("@CustomerID", SqlDbType.Int));
				this.insertCommand.Parameters.Add(new SqlParameter("@PaymentDate", SqlDbType.VarChar, 32));
				this.insertCommand.Parameters.Add(new SqlParameter("@PaymentProject", SqlDbType.NVarChar, 32));
				this.insertCommand.Parameters.Add(new SqlParameter("@TotalValue", SqlDbType.Money));
				this.insertCommand.Parameters.Add(new SqlParameter("@WayOfPayment", SqlDbType.NVarChar, 16));
				this.insertCommand.Parameters.Add(new SqlParameter("@PaymentAddress", SqlDbType.NVarChar, 64));
				this.insertCommand.Parameters.Add(new SqlParameter("@Remark", SqlDbType.NVarChar, 64));
				this.insertCommand.Parameters["@PKID"].Value = nPKID;
				this.insertCommand.Parameters["@CustomerID"].Value = nCustomerID;
				this.insertCommand.Parameters["@PaymentDate"].Value = dtPaymentDate.ToShortDateString();
				this.insertCommand.Parameters["@PaymentProject"].Value = paymentProject;
				this.insertCommand.Parameters["@TotalValue"].Value = totalValue;
				this.insertCommand.Parameters["@WayOfPayment"].Value = wayOfPayment;
				this.insertCommand.Parameters["@PaymentAddress"].Value = paymentAddress;
				this.insertCommand.Parameters["@Remark"].Value = remark;
				this.insertCommand.ExecuteNonQuery();
			}
			catch (SqlException ex)
			{
				throw ex;
			}
			return true;
		}

		public void Dispose()
		{
			this.Dispose(true);
			GC.SuppressFinalize(true);
		}

		protected virtual void Dispose(bool disposing)
		{
			if (disposing)
			{
				if (this.dataAdapter != null)
				{
					if (this.dataAdapter.SelectCommand != null)
					{
						if (this.dataAdapter.SelectCommand.Connection != null)
						{
							this.dataAdapter.SelectCommand.Connection.Dispose();
							this.dataAdapter.SelectCommand.Connection = null;
						}
						this.dataAdapter.SelectCommand.Dispose();
						this.dataAdapter.SelectCommand = null;
					}
					this.dataAdapter.Dispose();
					this.dataAdapter = null;
				}
				if (this.insertCommand != null)
				{
					if (this.insertCommand.Connection != null)
					{
						this.insertCommand.Connection.Dispose();
						this.insertCommand.Connection = null;
					}
					this.insertCommand.Dispose();
					this.insertCommand = null;
				}
				if (this.updateCommand != null)
				{
					if (this.updateCommand.Connection != null)
					{
						this.updateCommand.Connection.Dispose();
						this.updateCommand.Connection = null;
					}
					this.updateCommand.Dispose();
					this.updateCommand = null;
				}
				if (this.deleteCommand != null)
				{
					if (this.deleteCommand.Connection != null)
					{
						this.deleteCommand.Connection.Dispose();
						this.deleteCommand.Connection = null;
					}
					this.deleteCommand.Dispose();
					this.deleteCommand = null;
				}
			}
		}
	}
}
